package com.sunfeng.chap2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 验证long write 方法将ByteBuffer 的remaining（position到limit的区间）写入通道  123456bcty
 *
 */
public class Test3_2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:\\a.txt"));

        FileChannel fileChannel = fileOutputStream.getChannel();


        fileChannel.write(ByteBuffer.wrap("123456".getBytes()));
        ByteBuffer byteBuffer = ByteBuffer.wrap("abcdef".getBytes());
        ByteBuffer byteBuffer1 = ByteBuffer.wrap("qwertyu".getBytes());

        // 注意理解limit的含义
        ByteBuffer[] buffers = {byteBuffer, byteBuffer1};
        byteBuffer.position(1);
        byteBuffer.limit(3);//
        byteBuffer1.position(4);
        byteBuffer1.limit(6);
        fileChannel.write(buffers);

        fileChannel.close();
        fileOutputStream.close();

    }
}
